const { read, write } = require('../lib/read&write')

const BUGURL = '../data/bug.json'
function setBug(bug) {
  const __Bug = read(BUGURL)
  __Bug.push(bug)
  write(__Bug, BUGURL)
}

function editBug(bug) {
  const __Bug = read(BUGURL)
  __Bug.splice(
    __Bug.findIndex((item) => item.id == bug.id),
    1,
    bug,
  )
  write(__Bug, BUGURL)
}

function deleteBug(bugId) {
  const __Bug = read(BUGURL)
  let targetBug = searchBug(bugId)
  __Bug.splice(
    __Bug.findIndex((item) => item.id == targetBug.id),
    1
  )
  write(__Bug, BUGURL)
}

function getAllBugs() {
  const __Bug = read(BUGURL)
  return __Bug
}

function searchBug(bugId) {
  const __Bug = read(BUGURL)
  return __Bug.find((item) => item.id === bugId)
}

module.exports = {
  searchBug,
  getAllBugs,
  editBug,
  setBug,
  // 以下未用
  deleteBug,
}
